package com.longge;

import java.util.HashMap;

public class MaxLengthBetweenEqualCharacters {

    public int maxLengthBetweenEqualCharacters(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        char[] chars = s.toCharArray();
        int ans = -1;
        for (int i = 0; i < chars.length; i++) {
            Integer index = map.get(chars[i]);
            if (index != null) {
                ans = Math.max(i - index - 1, ans);
            } else {
                map.put(chars[i], i);
            }
        }
        return ans;
    }
}
